/**
 * 
 */
package com.servitware.cardio.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author erik
 * 
 */
public class ConfigurationManager
{
	/*
	 * parametro que pasa la aplicacion
	 * cada aplicacion tiene su nombre, fijado x politica 
	 */
	private String appName;
	private static ConfigurationManager cfgMangr = null;
	private Configuratio cfg = null;

	// variables del sistema
	private String fileSeparator = null;
	

	private ConfigurationManager( String appName )
	{
		this.appName=appName;
		fileSeparator = System.getProperty("file.separator");
		try
		{
			readConfig();
		}
		catch (Exception e)
		{
		}
	}

	public static ConfigurationManager getInstance(String appName)
	{
		if (cfgMangr == null)
			cfgMangr = new ConfigurationManager(appName);

		return cfgMangr;
	}

	public void saveConfig() throws IOException
	{
		FileOutputStream fileOut = new FileOutputStream( appName + ".data");

		ObjectOutputStream objOut = new ObjectOutputStream(fileOut);

		objOut.writeObject(cfg);
		
		objOut.close();
	}

	
	private void readConfig() throws IOException, FileNotFoundException, ClassNotFoundException
	{
		FileInputStream fileIn = new FileInputStream( appName + ".data");

		ObjectInputStream objIn = new ObjectInputStream(fileIn);

		cfg = (Configuratio) objIn.readObject();
		
		objIn.close();
	}

	public Configuratio getConfiguratio() throws IOException, FileNotFoundException, ClassNotFoundException
	{
		if( cfg == null )
			readConfig();
		
		return cfg;
	}


	public void setConfiguratio(Configuratio configuratio)
	{
		cfg = configuratio;
		try
		{
			saveConfig();	
		}
		catch (Exception e)
		{
		}
		
	}

	/**
	 * Chequea la existencia de la estructura de archivos
	 * 
	 * @return
	 * 		true si existe la estructura 
	 * 		false si no existe
	 */
	public boolean checkConfigDirs()
	{
		//TODO Comentado
		/*if( cfg == null )
			return false;
		
		File sinProcesar = getBaseDirSubDir(cfg.getBaseDir(), cfg.getDirSinProcesar());
		File enProceso = getBaseDirSubDir(cfg.getBaseDir(), cfg.getDirEnProceso());
		File procesado = getBaseDirSubDir(cfg.getBaseDir(), cfg.getDirProcesado());
		
		return sinProcesar.exists() && enProceso.exists() && procesado.exists();*/
		return true;
	}
	
	/**
	 * Retorna objeto File correspondiente a un subdirectorio del directorio base
	 * 
	 * @param baseDir
	 * @param subDir
	 * @return
	 * 			Objeto File
	 */
	private File getBaseDirSubDir ( File baseDir, String subDir )
	{
		return new File( baseDir.getAbsolutePath() + fileSeparator + subDir );
	}
	
	/**
	 * Crea la estructura de directorios de Configuratio
	 * 
	 * @return
	 * @throws NoAvailableConfigException
	 */
	public boolean createDirs() throws NoAvailableConfigException
	{
		/*
		 * leemos configuracion
		 */
		if( cfg == null )
		{
			try
			{
				readConfig();
			}
			catch (FileNotFoundException e)
			{
				throw new NoAvailableConfigException();
			}
			catch (ClassNotFoundException e)
			{
				e.printStackTrace();
				return false;
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		/*
		 * creamos los directorios
		 */
		File baseDir = cfg.getBaseDir();
		if( !baseDir.exists() )
		{
			baseDir.mkdir();
			
			/*File file = getBaseDirSubDir(baseDir, cfg.getDirSinProcesar() );
			file.mkdir();
			file = null;
			
			file =  getBaseDirSubDir(baseDir, cfg.getDirEnProceso() );
			file.mkdir();
			file = null;
			
			file =  getBaseDirSubDir(baseDir, cfg.getDirProcesado() );
			file.mkdir();
			file = null;*/
		}

		return true;
	}
		
//	public static void main(String[] args)
//	{
//		Configuratio cfg = Configuratio.getInstance();
//		cfg.setDirBase( "/home/erik/crdocstat" );
//		
//		ConfigurationManager cfgMang = ConfigurationManager.getInstance("crdocstat");
//		try
//		{
//			cfgMang.createDirs();
//		}
//		catch (NoAvailableConfigException e)
//		{
//			e.printStackTrace();
//		}
//		
//	}
}
